#include <iostream>

using namespace std;

typedef struct _QUEUE
{
	int head;
	int tail;
	int length;
	TREE_NODE** pHead;
}QUEUE;

QUEUE* create_queue_for_tree(const TREE_NODE* pTreeNode)
{
	QUEUE* pQueue;
	int count;

	if(NULL == pTreeNode)
		return NULL;

	count = count_all_node_number(pTreeNode);
	pQueue = (QUEUE*)malloc(sizeof(QUEUE));
	memset(pQueue, 0, sizeof(QUEUE));

	pQueue->pHead = (TREE_NODE**)malloc(sizeof(TREE_NODE*)*count);
	pQueue->head = pQueue->tail = 0;
	pQueue->length = count;
	return pQueue;
}

void insert_node_into_queue(QUEUE* pQueue, TREE_NODE* pNode)
{
	if(NULL == pQueue || NULL == pQueue->pHead || NULL == pNode)
		return;
	pQueue->pHead[pQueue->tail++] = pNode;
	return;
}

TREE_NODE* get_node_from_queue(QUEUE* pQueue)
{
	if(NULL == pQueue || NULL == pQueue->pHead)
		return NULL;
	if(pQueue->head == pQueue->tail)
		return NULL;
	return pQueue->pHead[pQueue->head++];
}

QUEUE* traverse_node_by_layer(TREE_NODE* pNode)
{
	QUEUE* pQueue;
	if(NULL == pNode)
		return NULL;
	pQueue = create_queue_for_tree(pNode);
	insert_node_into_queue(pQueue,pNode);
	pNode = get_node_from_queue(pQueue);

	while(pNode){
		if(pNode->left)
			insert_node_into_queue(pQueue,pNode->left);
		if(pNode->right)
			insert_node_into_queue(pQueue,pNode->right);
		pNode = get_node_from_queue(pQueue);
	}
	return pQueue;
}

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}
